<template>
  <div class="department-container">
    <div class="box">
      <el-tree :data="data" :props="defaultProps" default-expand-all>
        <!--先利用插槽后利用栅格系统来进行左右布局 -->
        <template v-slot="{ data }">
          <el-row style="width: 100%;">
            <el-col :span="20"><div>{{ data.name }}</div></el-col>
            <el-col :span="4">
              <div class="right-ele">
                <span class="right-name">{{ data.managerName }}</span>
                <el-dropdown class="re-drop" @command="dropdownTmp($event,data.id)">
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 正式思维来说，我们会给每一个都添加点击事件，这样不好，element-ui里面会有一些优化的手段，减少事件的绑定 -->
                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                    <el-dropdown-item command="delete">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <AllDialog
        ref="dialogRef"
        :is-edit.sync="isEdit"
        :show-dialog.sync="showDialog"
        :department-list="departmentList"
        :crrentid="crrentID"
        @addSuccess="companyDepartment"
      />
    </div>
  </div>
</template>

<script>
import { companyDepartmentRequest, deleteDepartmentRequest } from '@/api/department'
import { transformListToTree } from '@/utils/index.js'
import AllDialog from './components/allDialog.vue'
export default {
  name: 'Department',
  components: { AllDialog },
  data() {
    return {
      data: [],
      departmentList: [],
      crrentID: '',
      isEdit: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      showDialog: false
    }
  },
  created() {
    this.companyDepartment()
  },
  methods: {
    async companyDepartment() {
      const res = await companyDepartmentRequest()
      this.departmentList = res.data
      this.data = transformListToTree(res.data, 0)
      console.log(res)
    },
    dropdownTmp(type, id) {
      this.crrentID = id
      // console.log(type, id)
      if (type === 'add') {
        this.isEdit = false
        this.showDialog = true
      } else if (type === 'delete') {
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await deleteDepartmentRequest(id)
          this.companyDepartment()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.isEdit = true
        this.showDialog = true
        // 在这边调用子组件的方法不好，因为这样会破坏数据流向，建议使用数据的监听器
        // this.$nextTick(() => {
        //   this.$refs.dialogRef.getDepartment()
        // })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container{
  padding:10px ;
  font-size: 14px;
  min-height: calc(100vh - 80px);
  background-color: #f5f6f8;
  .box{
    padding: 30px 140px;
    background-color: #fff;

  }
  .right-ele{
    .re-drop{
      margin-left: 5px;
    }
  }
}
</style>
